﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using SimpleLPR2;

namespace LPRTest
{
    public partial class Form1 : Form
    {
        private string path = "";
        private int index = -1;

        public Form1()
        {
            InitializeComponent();
        }

        private void btBrowse_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog folderPicker = new FolderBrowserDialog();
            if (folderPicker.ShowDialog() == DialogResult.OK)
            {
                path = folderPicker.SelectedPath;
                listBox1.Items.Clear();

                string[] files = Directory.GetFiles(folderPicker.SelectedPath);
                foreach (string file in files)
                {

                    string fileName = Path.GetFileName(file);

                    listBox1.Items.Add(fileName);

                }

            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            listBox1.SelectedIndex = index + 1;

            Bitmap orig = new Bitmap(pictureBox1.Image);
            Bitmap clone = new Bitmap(orig.Width, orig.Height, System.Drawing.Imaging.PixelFormat.Format32bppRgb);
            using (Graphics gr = Graphics.FromImage(clone))
            {
                gr.DrawImage(orig, new Rectangle(0, 0, clone.Width, clone.Height));
            }

            ISimpleLPR lpr = SimpleLPR.Setup();
            // Set the product key
            //lpr.set_productKey("productkey.xml");
            // Enable Germany, disable Spain and United Kingdom
            //lpr.set_countryWeight("Germany", 1.0f);
            //lpr.set_countryWeight("Spain", 0.0f);
            //lpr.set_countryWeight("UK-GreatBritain", 0.0f);
            lpr.set_countryWeight("Vietnam", 1.0f);
            label1.Text = lpr.numSupportedCountries.ToString();
            // Apply changes
            lpr.realizeCountryWeights();

            // Create Processor
            IProcessor proc = lpr.createProcessor();
            // Process source file
            List<Candidate> cds = proc.analyze(clone, 120);
            if (cds.Count > 0)
            {
                // Iterate over all candidates
                foreach (Candidate cd in cds)
                {
                    label1.Text = cd.text;
                    label2.Text = cd.confidence.ToString();
                }
                //Console.WriteLine();
            }
            else
                label1.Text = "Nothing detected";
        }

        private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            index = listBox1.SelectedIndex;

            pictureBox1.Load(path + @"\" + listBox1.SelectedItem);
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (index <0) return;

            listBox1.SelectedIndex = index - 1;

            Bitmap orig = new Bitmap(pictureBox1.Image);
            Bitmap clone = new Bitmap(orig.Width, orig.Height, System.Drawing.Imaging.PixelFormat.Format32bppRgb);
            using (Graphics gr = Graphics.FromImage(clone))
            {
                gr.DrawImage(orig, new Rectangle(0, 0, clone.Width, clone.Height));
            }

            ISimpleLPR lpr = SimpleLPR.Setup();
            // Set the product key
            //lpr.set_productKey("productkey.xml");
            // Enable Germany, disable Spain and United Kingdom
            //lpr.set_countryWeight("Germany", 1.0f);
            //lpr.set_countryWeight("Spain", 0.0f);
            //lpr.set_countryWeight("UK-GreatBritain", 0.0f);
            lpr.set_countryWeight("Vietnam", 1.0f);
            label1.Text = lpr.numSupportedCountries.ToString();
            // Apply changes
            lpr.realizeCountryWeights();

            // Create Processor
            IProcessor proc = lpr.createProcessor();
            // Process source file
            List<Candidate> cds = proc.analyze(clone, 120);
            if (cds.Count > 0)
            {
                // Iterate over all candidates
                foreach (Candidate cd in cds)
                {
                    label1.Text = cd.text;
                    label2.Text = cd.confidence.ToString();
                }
                //Console.WriteLine();
            }
            else
                label1.Text = "Nothing detected";
        }
    }
}
